package com.voxel.sense.infomsg.widget.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.com.common.utils.ScreenUtils;
import com.example.com.common.utils.SizeUtils;
import com.example.com.common.view.adapter.TBaseAdapter;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.constants.Constants;
import com.voxel.sense.infomsg.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenYiYao on 2017/11/24.
 */

public class FileGriviewAdapter extends TBaseAdapter<String> {
    RelativeLayout.LayoutParams layoutParams;
    private int mImageSize;
    private int file_type = Constants.file_iage;//默认是图片


    public FileGriviewAdapter(Context context, List<String> list, int type) {
        super(context, list, R.layout.item_recycler_image);
        this.file_type = type;
        this.context = context;
        int pad = SizeUtils.dp2px(2);
        mImageSize = (ScreenUtils.getScreenWidth() - pad) / 4;
        //设置item 的大小
        layoutParams = new RelativeLayout.LayoutParams(mImageSize, mImageSize);
    }

    public FileGriviewAdapter(Context context, int type) {
        this(context, new ArrayList<String>(), type);
    }

    @Override
    public void initView(ViewHolder holder) {
        holder.addView(R.id.iv_pic);
    }

    @Override
    public void setViewValue(ViewHolder holder, final int position) {
        String path = list.get(position);
        ImageView iv_pic = holder.getImageView(R.id.iv_pic);
        iv_pic.setLayoutParams(layoutParams);
        View parent = holder.getParent();
        //点击删除图标，移除文件
        loadItemInfo(file_type, path, iv_pic, R.mipmap.add_image);

    }

    private void loadItemInfo(int file_type, String path, ImageView iv_pic, int addIconResId) {
        //文件item
        switch (file_type) {
            case Constants.file_docu://文档
                int i = path.lastIndexOf(".");
                String fileExtension = path.substring(i + 1, path.length());
                loadDocuIcon(iv_pic, fileExtension);
                break;
            case Constants.file_vedio://视频
                GlideUtils.GlideLoadPic(context, R.mipmap.mp4_icon, iv_pic);
                break;
            default://默认类型图片
                GlideUtils.GlideLoadPic(context, path, iv_pic);
                break;
        }
    }


    //初始化icon
    private void loadDocuIcon(ImageView iv_pic, String fileExtension) {
        if (Constants.EXTENSION_DOC.equals(fileExtension) || Constants.EXTENSION_DOCX.equals(fileExtension)) {
            //doc文档
            GlideUtils.GlideLoadPic(context, R.mipmap.word_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_XLS.equals(fileExtension) || Constants.EXTENSION_XLSX.equals(fileExtension)) {
            //xls文档
            GlideUtils.GlideLoadPic(context, R.mipmap.xls_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_PDF.equals(fileExtension)) {
            //pdf文档
            GlideUtils.GlideLoadPic(context, R.mipmap.pdf_doc_icon, iv_pic);
        } else if (Constants.EXTENSION_PPT.equals(fileExtension) || Constants.EXTENSION_PPTX.equals(fileExtension)) {
            //ppt文档
            GlideUtils.GlideLoadPic(context, R.mipmap.ppt_doc_icon, iv_pic);
        } else {
            //默认显示文本
            GlideUtils.GlideLoadPic(context, R.mipmap.text_icon, iv_pic);
        }
    }
}
